layui.use(['table', 'admin', 'ax', 'func', 'laydate'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    let laydate = layui.laydate;

    laydate.render({
        elem: '#activeDate'
        ,type: 'date'
        , range: '至' //或 range: '~' 来自定义分割字符
    });

    /**
     * 志愿活动信息表管理
     */
    var Active = {
        tableId: "activeTable"
    };

    /**
     * 初始化表格的列
     */
    Active.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: ''},
            {field: 'actId', hide: true, title: '活动id（北方网）'},
            {field: 'activeName', sort: true, title: '活动名称'},
            {field: 'chargePerson', hide: true, title: '负责人'},
            {field: 'summary', hide: true, title: '活动简介'},
            {field: 'place', hide: true, title: '活动地点'},
            {field: 'serviceType', hide: true, title: '服务类别（服务类别字典code）'},
            {field: 'startTime', sort: true, title: '开始时间'},
            {field: 'endTime', sort: true, title: '结束时间'},
            {field: 'expectDuration', hide: true, title: '预计活动时长'},
            {field: 'expectPoints', hide: true, title: '预计可得积分'},
            {field: 'deptId', hide: true, title: '发起单位'},
            {field: 'userId', hide: true, title: '发起人'},
            {field: 'status', hide: true, title: '活动状态   0：草稿 1:提交  2：通过 3：驳回'},
            {field: 'year', hide: true, title: '活动年度'},
            {field: 'createTime', hide: true, title: ''},
            {field: 'updateTime', hide: true, title: ''},
            {field: 'createUser', hide: true, title: ''},
            {field: 'updateUser', hide: true, title: ''},
            {field: 'delFlag', hide: true, title: ''},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    Active.search = function () {
        let queryData = {};
        queryData['betweenMonthStr'] = $("#activeDate").val();
        queryData['activeName'] = $("#activeName").val();
        table.reload(Active.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 弹出添加对话框
     */
    Active.openAddDlg = function () {
        func.open({
            title: '添加志愿活动信息表',
            content: Feng.ctxPath + '/active/add',
            tableId: Active.tableId
        });
    };

    /**
    * 点击编辑
    *
    * @param data 点击按钮时候的行数据
    */
    Active.openEditDlg = function (data) {
        func.open({
            title: '修改志愿活动信息表',
            content: Feng.ctxPath + '/active/edit?id=' + data.id,
            tableId: Active.tableId
        });
    };
    Active.onActMemberItem = function (data) {
        let index = func.open({
            title: data.activeName,
            content: Feng.ctxPath + '/activeMember?activeId=' + data.id,
            tableId: Active.tableId,
            maxmin:true,
            success: function(layero){
                //在回调方法中的第2个参数“index”表示的是当前弹窗的索引。
                //通过layer.full方法将窗口放大。
                layer.full();
            }
        });
        top.layer.full(index);
    };

    /**
     * 导出excel按钮
     */
    Active.exportExcel = function () {
        var checkRows = table.checkStatus(Active.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    Active.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/active/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(Active.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Active.tableId,
        url: Feng.ctxPath + '/active/list',
        page: true,
        height: "full-150",
        cellMinWidth: 100,
        cols: Active.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        Active.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        Active.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        Active.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + Active.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            Active.openEditDlg(data);
        } else if (layEvent === 'delete') {
            Active.onDeleteItem(data);
        }else if (layEvent === 'actMember') {
            Active.onActMemberItem(data);
        }
    });
});
